package com.kldy8.kldyw.system;

import android.content.Context;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.widget.Toast;

import com.kldy8.kldyw.release.entity.Pic;
import com.kldy8.kldyw.release.service.HttpCallback;
import com.kldy8.kldyw.release.service.HttpUtil;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import org.json.JSONException;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import okhttp3.Request;

/**
 * Created by Administrator on 2016/8/16.
 */
public class ServerApi
{
    /**
     *
     * @param username
     * @param password
     * @param phone_code
     * @param type_id
     * @param f_person （0个人/1企业）
     * @param callback
     */
    public static void registration(String username, String password, String phone_code,String f_person,String type_id,String client_key,
                                    ServerCallBack callback) {
        String url = UrlBuilder.URL_BASE_API + UrlBuilder.URL_MEMBER_REGISTRATION;
        Map<String, String> params = new HashMap<String, String>();
        params.put("username", username);
        params.put("password", password);
        params.put("phone_code", phone_code);
        params.put("type", type_id);
        params.put("f_person", f_person);
        params.put("device_name", "Android");
        params.put("client_key", client_key);

        Log.e("params",params.toString());
//        if (file != null) {
//            OkHttpUtils.post().url(url).params(params).addFile("headImage", file.getName(), file).build().execute(callback);
//        } else {
        OkHttpUtils.post().url(url).params(params).build().execute(callback);
//        }
    }


    /**
     * 登录接口
     * @param username 用户名
     * @param password 密码
     * @param client_key 设备号
     * device_name device_name string 设备名称（Android/IPhone/IPad）
     * @param callback
     */
    public static void login(String username, String password, String client_key ,ServerCallBack callback)
    {
        Log.e("username",username);
        Log.e("password",password);
        String url = UrlBuilder.URL_BASE_API + UrlBuilder.URL_MEMBER_LOGIN;
        Map<String, String> params = new HashMap<String, String>();
        params.put("username", username);
        params.put("password", password);
        params.put("client_key", client_key);
        params.put("device_name", "Android");
        OkHttpUtils.post().url(url).params(params).build().execute(callback);
    }

    /**
     *  登录接口
     * @param userGender
     * @param userIcon
     * @param userId
     * @param userName
     * @param client_key
     * @param callback
     */
    public static void login(String token,String userGender, String userIcon, String userId, String userName,String client_key ,ServerCallBack callback)
    {
        Log.e("token",token);
        Log.e("userGender",userGender);
        Log.e("userIcon",userIcon);
        Log.e("userId",userId);
        Log.e("userName",userName);
        Log.e("client_key",client_key);

        String url = UrlBuilder.URL_BASE_API + UrlBuilder.URL_MEMBER_LOGIN;
        Map<String, String> params = new HashMap<String, String>();
        params.put("userGender", userGender);

        params.put("userGender", userGender);
        params.put("userIcon", userIcon);
        params.put("userId", userId);
        params.put("userName", userName);
        params.put("client_key", client_key);
        params.put("device_name", "Android");
        OkHttpUtils.post().url(url).params(params).build().execute(callback);
    }


    /**
     * 手机验证码接口
     * @param username
     * @param type    register注册，initpassword忘记密码，bind_phone绑定手机号
     * @param callback
     */
    public final static String  SMS_TYPE_REGISTER = "register";
    public final static String  SMS_TYPE_INITPASSWORD  ="initpassword";
    public final static String  SMS_TYPE_BIND_PHONE  ="bind_phone";

    public static void SMS(String username,String type,ServerCallBack callback)
    {
        String url = UrlBuilder.URL_BASE_API + UrlBuilder.URL_MEMBER_SMS;
        Map<String, String> params = new HashMap<String, String>();
        params.put("username", username);
        params.put("type", type);
        OkHttpUtils.get().url(url).params(params).build().execute(callback);
    }

    /**
     *
     * @param password_o  旧密码
     * @param password_n  新密码
     * @param callback
     */
    public static void password(String password_o,String password_n ,ServerCallBack callback)
    {
        String token = "";
        String url = UrlBuilder.URL_BASE_API + UrlBuilder.URL_MEMBER_PASSWORD+token;
        Map<String, String> params = new HashMap<String, String>();
        params.put("password_o", password_o);
        params.put("password_n", password_n);
        OkHttpUtils.post().url(url).params(params).build().execute(callback);
    }

    public static String  ID_NEWSS_TRAINING="4";
    public static String  ID_NEWSS_EXPERIENCE  ="5";
    public static String  ID_NEWSS_CHARACTER  ="6";
    public static String  ID_NEWSS_NEWS  ="7";
    public static String  ID_NEWSS_OBSERVATION ="8";
    public static String  ID_NEWSS_SEEDING ="9";


    public static void newss(String column_id,String page,ServerCallBack callback)
    {
        String url =UrlBuilder.URL_BASE_API + UrlBuilder.URL_MEMBER_NEWSS;
        Map<String, String> params = new HashMap<String, String>();
        params.put("column_id", column_id);
        params.put("page", page);
        Log.e("newss:params",params.toString());
        OkHttpUtils.get().url(url).params(params).build().execute(callback);
    }

    public static void comment(String token,String news_id,String content,ServerCallBack callback)
    {
        String url =UrlBuilder.URL_BASE_API + UrlBuilder.URL_MEMBER_COMMENT+token;
        Map<String, String> params = new HashMap<String, String>();
        params.put("news_id", news_id);
        params.put("content", content);
        OkHttpUtils.get().url(url).params(params).build().execute(callback);
    }

    public static void initpassword(String username,String password,String phone_code,ServerCallBack callback)
    {
        String url =UrlBuilder.URL_BASE_API + UrlBuilder.URL_MEMBER_INITPASSWORD;
        Map<String, String> params = new HashMap<String, String>();
        params.put("username", username);
        params.put("password", password);
        params.put("phone_code", phone_code);
        Log.e("params",""+params);
        OkHttpUtils.post().url(url).params(params).build().execute(callback);
    }

    /**
     * 人才求职列表接口
     * @param page
     * @param position
     * @param city
     * @param callback
     */
    public static void person_list(String page,String position,String city,ServerCallBack callback)
    {
        String url =UrlBuilder.URL_BASE_API + UrlBuilder.URL_PERSON_LIST;
        Map<String, String> params = new HashMap<String, String>();
        params.put("page", page);
        params.put("position", position);
        params.put("city", city);
        Log.e("url",url);
        OkHttpUtils.get().url(url).params(params).build().execute(callback);
    }

    /**
     * 人才招募列表
     * @param page
     * @param position
     * @param city
     * @param callback
     */
    public static void job_company_list(String page,String position,String city,ServerCallBack callback)
    {
        String url =UrlBuilder.URL_BASE_API + UrlBuilder.URL_JOB_COMPANY_LIST;
        Map<String, String> params = new HashMap<String, String>();
        params.put("page", page);
        params.put("position", position);
        params.put("city", city);
        OkHttpUtils.get().url(url).params(params).build().execute(callback);
    }

    /**
     * page int 页码（选填）
     * category int 场地类型编号（选填）
     * city int 地区编号（选填）
     * @param page
     * @param category
     * @param city
     * @param price（1:100以下,2:100-500,3:500-1000,3:1000-2000,4:2000-5000,5:5000-10000,6:10000以上，如2为100<=price<500）
     * @param callback
     */
    public static void list_out(String page,String category,String city,String price,ServerCallBack callback)
    {
        String url =UrlBuilder.URL_BASE_API + UrlBuilder.URL_LIST_OUT;
        Map<String, String> params = new HashMap<String, String>();
        params.put("page", page);
        params.put("category", category);
        params.put("city", city);
        params.put("price", price);
        OkHttpUtils.get().url(url).params(params).build().execute(callback);
    }

    /**
     * page int 页码（选填）
     * category int 场地类型编号（选填）
     * city int 地区编号（选填）
     * @param page
     * @param category
     * @param city
     * @param price（1:100以下,2:100-500,3:500-1000,3:1000-2000,4:2000-5000,5:5000-10000,6:10000以上，如2为100<=price<500）
     * @param callback
     */
    public static void list_in(String page,String category,String city,String price,ServerCallBack callback)
    {
        String url =UrlBuilder.URL_BASE_API + UrlBuilder.URL_LIST_IN;
        Map<String, String> params = new HashMap<String, String>();
        params.put("page", page);
        params.put("category", category);
        params.put("city", city);
        params.put("price", price);
        OkHttpUtils.get().url(url).params(params).build().execute(callback);
    }


    /**
     * 添加直播
     * @param member_id
     * @param title
     * @param callback
     */
    public static void live_add(String member_id,String title,ServerCallBack callback)
    {
        String url = UrlBuilder.URL_BASE_API + UrlBuilder.URL_LIVE_ADD;
        Map<String, String> params = new HashMap<String, String>();
        params.put("member_id", member_id);
        params.put("title", title);
        OkHttpUtils.post().url(url).params(params).build().execute(callback);
    }

    /**
     * 直播更新
     * @param live_id
     * @param callback
     */
    public static void live_update(String live_id ,String member_id ,String url_pc
            ,String url_mobile  ,ServerCallBack callback)
    {
        String url = UrlBuilder.URL_BASE_API + UrlBuilder.URL_LIVE_UPDATE;
        Map<String, String> params = new HashMap<String, String>();
        params.put("live_id", live_id);
        params.put("member_id", member_id);
        params.put("url_pc", url_pc);
        params.put("url_mobile", url_mobile);
        params.put("state", "1");
        OkHttpUtils.post().url(url).params(params).build().execute(callback);
    }

    /**
     * 修改直播状态
     * @param live_id
     * @param callback
     */
    public static void live_edit_status(String token,String live_id,ServerCallBack callback)
    {
        String url =UrlBuilder.URL_BASE_API + UrlBuilder.URL_LIVE_EDIT_STATUS;
        Map<String, String> params = new HashMap<String, String>();
        params.put("token", token);
        params.put("live_id", live_id);
        OkHttpUtils.post().url(url).params(params).build().execute(callback);
    }

    /**
     *
     * @param page       页码（默认1）
     * @param order      排序：read人气/add_time时间
     * @param sort      排序方式：a升序/d降序
     * @param type_id   类型id
     * @param theme_id  主题id
     * @param age_id    年代id
     * @param callback
     */
    public static void script_list(String page ,String order ,String sort,String type_id
            ,String theme_id,String age_id,ServerCallBack callback)
    {
        String url = UrlBuilder.URL_BASE_API + UrlBuilder.URL_SCRIPT_LIST;
        Map<String, String> params = new HashMap<String, String>();
        params.put("page",page);
        params.put("order",order);
        params.put("sort",sort);
        params.put("type_id",type_id);
        params.put("theme_id",theme_id);
        params.put("age_id",age_id);
        OkHttpUtils.get().url(url).params(params).build().execute(callback);
    }


    public static void levy_list(String page ,String order ,String sort,String type_id
            ,String theme_id,String age_id,String search,ServerCallBack callback)
    {
        String url = UrlBuilder.URL_BASE_API + UrlBuilder.URL_LEVY_LIST;
        Map<String, String> params = new HashMap<String, String>();
        params.put("page",page);
        params.put("order",order);
        params.put("sort",sort);
        params.put("type_id",type_id);
        params.put("theme_id",theme_id);
        params.put("age_id",age_id);
        params.put("search",search);
        OkHttpUtils.get().url(url).params(params).build().execute(callback);
    }


    public static void device_list_loan(String page ,String category ,String city,String price
           ,ServerCallBack callback)
    {
        String url = UrlBuilder.URL_BASE_API + UrlBuilder.URL_DEVICE_LIST_LOAN;
        Map<String, String> params = new HashMap<String, String>();
        params.put("page",page);
        params.put("category",category);
        params.put("city",city);
        params.put("price",price);
        OkHttpUtils.get().url(url).params(params).build().execute(callback);
    }


    public static void device_list_borrow(String page  ,String category ,String city,String price,
                                          ServerCallBack callback)
    {
        String url = UrlBuilder.URL_BASE_API + UrlBuilder.URL_DEVICE_LIST_BORROW;
        Map<String, String> params = new HashMap<String, String>();
        params.put("page",page);
        params.put("category",category);
        params.put("city",city);
        params.put("price",price);
        OkHttpUtils.get().url(url).params(params).build().execute(callback);
    }


    public static void device_list_sell(String page ,String category ,String city,String price,ServerCallBack callback)
    {
        String url = UrlBuilder.URL_BASE_API + UrlBuilder.URL_DEVICE_LIST_SELL;
        Map<String, String> params = new HashMap<String, String>();
        params.put("page",page);
        params.put("category",category);
        params.put("city",city);
        params.put("price",price);
        Log.e("device_list_sell:params",params.toString());
        OkHttpUtils.get().url(url).params(params).build().execute(callback);
    }

    public static final String CP_REPAY = "cp_repay";
    public static final String CP_CURRENT_CNT = "cp_current_cnt";
    /**
     * 影视项目列表
     * @param page
     * @param order
     * @param sort
     * @param cp_hoot_date
     * @param cp_release_time
     * @param search
     * @param callback
     */
    public static void chips_list(String page ,String order ,String sort,String cp_hoot_date,String cp_release_time,String search,ServerCallBack callback)
    {
        String url = UrlBuilder.URL_BASE_API + UrlBuilder.URL_CHIPS_LIST;
        Map<String, String> params = new HashMap<String, String>();
        params.put("page",page);
        params.put("order",order);
        params.put("sort",sort);
        params.put("cp_hoot_date",cp_hoot_date);
        params.put("cp_release_time",cp_release_time);
        params.put("search",search);
        Log.e("chips_list:params",params.toString());
        OkHttpUtils.get().url(url).params(params).build().execute(callback);
    }

    /**
     * 公司列表
     * @param page
     * @param type_id
     * @param callback
     */
    public static void company_list(String page ,String type_id ,ServerCallBack callback)
    {
        String url = UrlBuilder.URL_BASE_API + UrlBuilder.URL_COMPANY_LIST;
        Map<String, String> params = new HashMap<String, String>();
        params.put("page",page);
        params.put("type_id",type_id);
        Log.e("company_list:params",params.toString());
        OkHttpUtils.get().url(url).params(params).build().execute(callback);
    }


    /**
     * 会员中心
     * @param token
     * @param callback
     */
    public static void center(String token,ServerCallBack callback)
    {
        String url = UrlBuilder.URL_BASE_API + UrlBuilder.URL_USER_CENTER;
        Map<String, String> params = new HashMap<String, String>();
        params.put("token",token);
        OkHttpUtils.get().url(url).params(params).build().execute(callback);
    }

    public static void logout(String token,ServerCallBack callback)
    {
        Log.e("token",token);
        String url = UrlBuilder.URL_BASE_API + UrlBuilder.URL_MEMBER_LOGOUT;
        Map<String, String> params = new HashMap<String, String>();
        params.put("token", token);
        OkHttpUtils.post().url(url).params(params).build().execute(callback);
    }


    public static void company_filter(ServerCallBack callback)
    {
        String url =UrlBuilder.URL_BASE_API + UrlBuilder.URL_COMPANY_FILTER;
        Map<String, String> params = new HashMap<String, String>();
        OkHttpUtils.get().url(url).params(params).build().execute(callback);
    }


    public static void dteam_list(String page,ServerCallBack callback)
    {
        String url =UrlBuilder.URL_BASE_API + UrlBuilder.URL_DTEAM_DTEAM;
        Map<String, String> params = new HashMap<String, String>();
        params.put("page", page);
        OkHttpUtils.get().url(url).params(params).build().execute(callback);
    }

    /**
     *
     * title    string    标题
     * image    string    封面图
     * works_name   string 作品名
     * position    int  招募人才类型
     * experience   int  经验要求：0-不限，1-0~1年，2-1~3年，3-3~5年，4-5~10年，5-10年以上
     * city    int 城市
     * education   int 学历：0不限，1高中，2专科，3本科，4研究生，5硕士以上
     * language   string  语音要求
     * start_time     date   开始时间
     * end_time       date      结束时间
     * price           decimal(10,2)            价格
     * price_unit      int      价格单位（0天，1月）
     * description     string     职责等具体描述
     * image_box    array    提交图片数组
     */
    public static void job_company_form(String title, String image, String works_name, String position, String experience, String city,
                                        String education, String language, String start_time, String end_time,String price,
                                        String price_unit,String description,List<Pic> picList,ServerCallBack callback)
    {
        String url =UrlBuilder.URL_BASE_API + UrlBuilder.URL_RELEASE_JOB_COMPANY_FORM;
        Map<String, String> params = new HashMap<String, String>();
        params.put("title", title);
        params.put("image", image);
        params.put("works_name", works_name);
        params.put("position", position);
        params.put("experience", experience);
        params.put("city", city);
        params.put("education", education);
        params.put("language", language);
        params.put("start_time", start_time);
        params.put("end_time", end_time);
        params.put("price", price);
        params.put("price_unit", price_unit);
        params.put("description", description);
        params.put("image_box", Pic.getPicNameListJson(picList));

        OkHttpUtils.get().url(url).params(params).build().execute(callback);
    }


    public static void company_form_view(ServerCallBack callback)
    {
        String url =UrlBuilder.URL_BASE_API + UrlBuilder.URL_RELEASE_COMPANY_FORM_VIEW;
        Map<String, String> params = new HashMap<String, String>();
        OkHttpUtils.get().url(url).params(params).build().execute(callback);
    }

//
//    public static void script_form_view(ServerCallBack callback)
//    {
//        String url =UrlBuilder.URL_BASE_API + UrlBuilder.URL_RELEASE_SCRIPT_FORM_VIEW;
//        Map<String, String> params = new HashMap<String, String>();
//        OkHttpUtils.get().url(url).params(params).build().execute(callback);
//    }


    public static void release_dteam_form_view(ServerCallBack callback)
    {
        String url =UrlBuilder.URL_BASE_API + UrlBuilder.URL_RELEASE_DTEAM_FORM_VIEW;
        Map<String, String> params = new HashMap<String, String>();
        OkHttpUtils.get().url(url).params(params).build().execute(callback);
    }


    /**
     * dramatic_team_id 非必选
     'works_name'    string   作品名称
     'works_type'     int   影视类型
     'image'  封面图
     'city' 城市
     'director'      导演
     'screenwriter'    string   编辑
     'producer' =>   string  监制
     'formation_addr'  组建地址
     'interview_addr'   面试地址
     'investment_amount'    金额
     'production_company'   出品公司
     'description'  描述
     'start_time'  开始时间
     'end_time'   结束时间
     'tel'   剧组负责人电话

     * @param callback
     */
    //ServerCallBack callback
    public static void release_dteam_form(String dramatic_team_id,String works_name, String works_type,String image, String city, String director, String screenwriter, String producer,
                                          String formation_addr, String interview_addr, String investment_amount, String production_company,String description,
                                          String start_time,String end_time,String tel, List<Pic> picList,HttpCallback callback)
    {
        String url =UrlBuilder.URL_BASE_API + UrlBuilder.URL_RELEASE_DTEAM_FORM;
        Map<String, String> params = new HashMap<String, String>();
        params.put("dramatic_team_id", dramatic_team_id);
        params.put("works_name", works_name);
        params.put("works_type", works_type);
        params.put("image", image);
        params.put("city", city);
        params.put("director", director);
        params.put("screenwriter", screenwriter);
        params.put("producer", producer);
        params.put("formation_addr", formation_addr);
        params.put("interview_addr", interview_addr);
        params.put("investment_amount", investment_amount);
        params.put("production_company", production_company);
        params.put("description", description);
        params.put("start_time", start_time);
        params.put("end_time", end_time);
        params.put("tel", tel);
        params.put("image_box", Pic.getPicNameListJson(picList));
        HttpUtil.post(url, params, callback);
//        OkHttpUtils.post().url(url).params(params).build().execute(callback);
    }


    public static void message_count(String token,ServerCallBack callback)
    {
        String url =UrlBuilder.URL_BASE_API + UrlBuilder.URL_MESSAGE_COUNT;
        Map<String, String> params = new HashMap<String, String>();
        params.put("token", token);
        OkHttpUtils.get().url(url).params(params).build().execute(callback);
    }

    public static void version(int version_code,ServerCallBack callback)
    {
        String url =UrlBuilder.URL_BASE_API + UrlBuilder.URL_UPDATE_VERSION;
        Map<String, String> params = new HashMap<String, String>();
        params.put("version_code",""+version_code);
        params.put("platform", "android");
        Log.e("TAG",params.toString());
        OkHttpUtils.get().url(url).params(params).build().execute(callback);
    }

    public static void actorVideo(String token, String title, String description, File file,String is_track,String video_imag_url, ServerCallBack callback) throws Exception {
        String url = UrlBuilder.URL_BASE_API + UrlBuilder.URL_ACTOR_VIDEO+"?token="+token;
        Map<String, String> params = new HashMap<String, String>();
        params.put("title", title);
        params.put("description", description);
        params.put("is_track", ""+is_track);
        params.put("thumbnail", video_imag_url);
        Log.e("TAG",params.toString());
        OkHttpUtils.post().url(url).params(params).addFile("file", file.getName(),file).build().execute(callback);
    }

    public static void wechat(String access_token, String openid, String client_key , ServerCallBack callback)  {
        String url = UrlBuilder.URL_BASE_API + UrlBuilder.URL_THIRDLOGIN_WECHAT;
        Map<String, String> params = new HashMap<String, String>();
        params.put("access_token", access_token);
        params.put("openid", openid);
        params.put("client_key", client_key);
        params.put("device_name", "Android");
        Log.e("wechat",params.toString());
        OkHttpUtils.post().url(url).params(params).build().execute(callback);
    }


    public static void weibo(String access_token, String uid, String client_key , ServerCallBack callback)  {
        String url = UrlBuilder.URL_BASE_API + UrlBuilder.URL_THIRDLOGIN_WEIBO;
        Map<String, String> params = new HashMap<String, String>();
        params.put("access_token", access_token);
        params.put("uid", uid);
        params.put("client_key", client_key);
        params.put("device_name", "Android");
        Log.e("wechat",params.toString());
        OkHttpUtils.post().url(url).params(params).build().execute(callback);
    }


    public static void thirdloginRegister(String register_way, String username, String phone_code, String type, String f_person, String client_key, String access_token, String openId,ServerCallBack callback)
    {
        String url = UrlBuilder.URL_BASE_API + UrlBuilder.URL_THIRDLOGIN_REGISTER;
        Map<String, String> params = new HashMap<String, String>();
        params.put("register_way", register_way);
        params.put("username", username);
        params.put("phone_code", phone_code);
        params.put("type", type);
        params.put("f_person", f_person);
        params.put("device_name", "Android");
        params.put("client_key", ""+client_key);
        params.put("access_token", ""+access_token);
        if ("wechat".equals(register_way)){
        params.put("openid", openId);}
        else  if ("weibo".equals(register_way)) {
            params.put("uid", openId);
        }

        Log.e("thirdloginRegister",params.toString());
        OkHttpUtils.post().url(url).params(params).build().execute(callback);

    }


    public static void cancelAll()
    {
        OkHttpUtils.getInstance().getOkHttpClient().dispatcher().cancelAll();
    }


}
